package com.ke.jdyd.ui.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;

import java.util.List;

public class HeaderPagerAdapter extends PagerAdapter {

    private List<String> images;

    private HeaderImageClickListener clickListener;

    public HeaderPagerAdapter(List<String> images, @Nullable HeaderImageClickListener clickListener) {
        this.images = images;
        this.clickListener = clickListener;
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        ImageView imageView = new ImageView(container.getContext());

        Glide.with(container.getContext())
                .load(images.get(position))
                .into(imageView);

        if (clickListener != null) {
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onClick(position, images.get(position));
                }
            });
        }

        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    public interface HeaderImageClickListener {
        void onClick(int position, String imageUrl);
    }
}

